-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fweinberger/align shutdown with spec #1091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 tasks
3ba87ad
to
9f4696d
Compare
The MCP specification recommends closing stdin first to allow servers to exit gracefully before resorting to signals. This approach gives well-behaved servers the opportunity to detect stdin closure and perform clean shutdown without forceful termination. The shutdown sequence now follows a graceful escalation path: first closing stdin and waiting 2 seconds for voluntary exit, then sending SIGTERM if needed, and finally using SIGKILL as a last resort. This minimizes the risk of data loss or corruption while ensuring cleanup always completes. This unified approach works consistently across all platforms and improves compatibility with MCP servers that monitor stdin for lifecycle management. resolves #765 Co-authored-by: davenpi <[email protected]>
9f4696d
to
757d154
Compare
@dsp-ant rework of this change we discussed offline. |
9 tasks
felixweinberger
added a commit
that referenced
this pull request
Jul 8, 2025
This test shows that MCP server cleanup code in lifespan doesn't run when the process is terminated, but does run when stdin is closed first (as implemented in PR #1044). The test includes: - Demonstration of current broken behavior (cleanup doesn't run) - Verification that stdin closure allows graceful shutdown - Windows-specific ResourceWarning handling - Detailed documentation of the issue and solution Github-Issue:#1027
felixweinberger
added a commit
that referenced
this pull request
Jul 8, 2025
This test shows that MCP server cleanup code in lifespan doesn't run when the process is terminated, but does run when stdin is closed first (as implemented in PR #1044). The test includes: - Demonstration of current broken behavior (cleanup doesn't run) - Verification that stdin closure allows graceful shutdown - Windows-specific ResourceWarning handling - Detailed documentation of the issue and solution Github-Issue:#1027
felixweinberger
added a commit
that referenced
this pull request
Jul 8, 2025
This test shows that MCP server cleanup code in lifespan doesn't run when the process is terminated, but does run when stdin is closed first (as implemented in PR #1044). The test includes: - Demonstration of current broken behavior (cleanup doesn't run) - Verification that stdin closure allows graceful shutdown - Windows-specific ResourceWarning handling - Detailed documentation of the issue and solution Github-Issue:#1027
9 tasks
bhosmer-ant
approved these changes
Jul 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
saqadri
pushed a commit
to saqadri/stdio-fixes
that referenced
this pull request
Aug 6, 2025
Co-authored-by: davenpi <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation and Context
This PR implements the MCP spec-compliant stdio shutdown sequence as defined in the MCP specification.
The MCP specification recommends that stdio transport clients should:
This approach gives well-behaved servers the opportunity to detect stdin closure and perform clean shutdown without forceful termination, minimizing the risk of data loss or corruption while ensuring cleanup always completes.
Resolves #765
How Has This Been Tested?
test_stdio_client_graceful_stdin_exit
: Verifies processes that monitor stdin exit gracefullytest_stdio_client_stdin_close_ignored
: Verifies escalation to SIGTERM when stdin closure is ignoredTypes of changes
Checklist
Implementation Details
The shutdown sequence now follows a graceful escalation path:
_terminate_process_tree
which handles:This unified approach works consistently across all platforms and improves compatibility with MCP servers that monitor stdin for lifecycle management.
The implementation leverages the existing platform-specific process termination utilities introduced in PRs #1044 and #1078, ensuring robust child process cleanup on both Windows (using Job Objects) and POSIX systems (using process groups).
Co-authored-by: davenpi [email protected]